package com.mfl.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mfl.common.PageResult;
import com.mfl.context.BaseContext;
import com.mfl.mapper.PostRemarkMapper;
import com.mfl.mapper.UserMapper;
import com.mfl.pojo.dto.PostRemarkDTO;
import com.mfl.pojo.dto.PostRemarkPageQueryDTO;
import com.mfl.pojo.entity.PostRemark;
import com.mfl.pojo.entity.User;
import com.mfl.pojo.vo.PostRemarkPageQueryVO;
import com.mfl.service.PostRemarkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class PostRemarkServiceImpl implements PostRemarkService {

    @Autowired
    private PostRemarkMapper postRemarkMapper;
    @Autowired
    private UserMapper userMapper;

    /**
     * 新增帖子回复
     * @param postRemarkDTO
     * @return
     */
    @Override
    public void insert(PostRemarkDTO postRemarkDTO) {
        PostRemark postRemark = PostRemark.builder()
                .postId(postRemarkDTO.getPostId())
                .userId(BaseContext.getCurrentId())
                .content(postRemarkDTO.getContent())
                .createTime(LocalDateTime.now())
                .build();
        postRemarkMapper.insert(postRemark);
    }

    /**
     * 分页查询帖子回复
     * @param postRemarkPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(PostRemarkPageQueryDTO postRemarkPageQueryDTO) {
        PageHelper.startPage(postRemarkPageQueryDTO.getPage(), postRemarkPageQueryDTO.getPageSize());
        Page<PostRemarkPageQueryVO> page = postRemarkMapper.pageQuery(postRemarkPageQueryDTO);
        page.getResult().forEach(item -> {
            User user = userMapper.getById(item.getUserId());
            item.setUserName(user.getName());
            item.setAvatar(user.getAvatar());
        });
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 批量删除帖子回复
     * @param ids
     */
    @Override
    public void batchDelete(List<Integer> ids) {
       postRemarkMapper.deleteByIds(ids);
    }
}
